//
//  CCDB.h
//  CCKit
//
//  Created by tucici on 2019/11/28.
//  Copyright © 2019 tucici. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
///create table if not exists stu(zfID text primary key,goodsID text,receipt text,isDid text,time text)
#define CCDB_create(talbleName,keysAndValues) [NSString stringWithFormat:@" create table if not exists %@(%@)",(talbleName),(keysAndValues)]

///insert into stu(stu_name,stu_age)values(‘YYY’,30);
#define CCDB_insert(tableName,keys,values) [NSString stringWithFormat:@"insert into %@ (%@) values (%@)",(tableName),(keys),(values)]

/// delete from stu where su_age=10;
#define CCDB_delete(tableName,key,value) [NSString stringWithFormat:@"delete from %@ where %@='%@'",(tableName),(key),(value)]

/// update stu set su_age=100 where stu_age=10; 条件: 可以一个或者多个条件(多个条件用     and(与)，or(或)     连接)
#define CCDB_update(tableName,newKey,newValue,oldKey,oldValue) [NSString stringWithFormat:@"update %@ set %@='%@' where %@='%@'",tableName,(newKey),(newValue),(oldKey),(oldValue)]

/// select stu_name,su_age from stu where s_sex=’男’;
#define CCDB_select(key,tableName,conditionKey,conditionValue) [NSString stringWithFormat:@"select %@ from %@ where %@='%@'",(key), (tableName), (conditionKey),(conditionValue)]

///select *from stu where su_sex = ‘XXX’;要查找的字段: 如果查找所有字段可以使用通配符  *
#define CCDB_selectAll(tableName,conditionKey,conditionValue) [NSString stringWithFormat:@"select * from %@ where %@='%@'",(key), (tableName), (conditionKey),(conditionValue)]

NS_ASSUME_NONNULL_BEGIN
@interface CCDB : NSObject

/// 数据库单例方法
+ (instancetype)db;
/// 打开数据库
/// @param name 库名，例如 数据库Receipt.sqlite，name=Receipt
- (void)openDM:(NSString *)name;

/// 添加表格
/// @param sql_create 添加表格的语句
- (void)createTable:(NSString *)sql_create;

/// 插入数据
/// @param tableName 插入数据的表名
/// @param dict 插入的是字典
- (void)insert:(NSString *)tableName withDict:(NSDictionary *)dict;

/// 插入数据
/// @param tableName 插入数据的表名
/// @param array 插入的是数组
- (void)insert:(NSString *)tableName withArray:(NSArray *)array;

/// 插入数据
/// @param sql_insert 插入数据语句
- (void)insert:(NSString *)sql_insert;
/// 删除数据
/// @param sql_delet 删除数据语句
- (void)delet:(NSString *)sql_delet;

/// 更改数据
/// @param sql_update 更改数据语句
- (void)update:(NSString *)sql_update;

/// 查询数据
/// @param sql_select 查询数据语句
- (NSMutableArray *)select:(NSString *)sql_select;

/// 关闭数据库
- (void)closeDB;





/// 创建表
/// @param tableName 要创建的表名，
/// @param array 字段数组
- (void)createTable:(NSString *)tableName withOptions:(NSArray *)array;

/// 更新数据
/// @param tableName 要更新数据的表名
/// @param dict 要更新的数据，以字典形式传递
/// @param conditionsDict 被更新数据的条件，以字典的形式传递，例如 where age='18', 字典传递conditionsDict=@{@"age":@"'18'"}
- (void)update:(NSString *)tableName withDict:(NSDictionary *)dict conditionsDict:(NSDictionary*)conditionsDict;

/// 删除数据
/// @param tableName 要删除数据的表名
/// @param dict 被删除数据的条件，以字典形式传递，例如 where age='18'，字典传递dict=@{@"age"=@"'18'"}
- (void)delet:(NSString *)tableName withDict:(NSDictionary *)dict;

/// 查找数据
/// @param tableName 要查找数据的表名
/// @param array 要查找的字段名，以数组形式传递，例如 要查找表里的age,name,height三个字段数据，array=@[@"age",@"name",@"height"];
/// @param conditionsDict 被查找的条件，以字典形式传递，例如 where Gender='male'
/// @param block 返回查找的数据，以数组形式返回
- (void)select:(NSString *)tableName withOptions:(nullable NSArray *)array conditionsDict:(NSDictionary*)conditionsDict complete:(void(^) (NSMutableArray *array))block;

/// 查找数据
/// @param tableName 要查找数据的表名
/// @param array 要查找的字段名，以数组形式传递，例如 要查找表里的age,name,height三个字段数据，array=@[@"age",@"name",@"height"];
/// @param conditionsDict 被查找的条件，以字典形式传递，例如 where Gender='male'
/// @return array 返回查找的数据，以数组形式返回
- (NSMutableArray *)select:(NSString *)tableName withOptions:(nullable NSArray *)array conditionsDict:(NSDictionary*)conditionsDict;

//- (void)insertData:(NSDictionary *)dataDict withName:(NSString *)tableName;

@end

NS_ASSUME_NONNULL_END
